package com.tiantian.quanminxiu.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

/**
 * Created by admin on 2016/9/20.
 */
public class PhotoUtils {

    //region 压缩图片的静态方法 compressImage(String path, String newPath)
    public static String compressImage(String path, String newPath) {

        int degree = getImageRotate(path);

        BitmapFactory.Options options = new BitmapFactory.Options();
        //是否显示边框的宽度
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int inSampleSize = 1;
        int maxSize = 500;
        if (options.outWidth > maxSize || options.outHeight > maxSize) {
            int widthScale = (int) Math.ceil(options.outWidth * 1.0 / maxSize);
            int heightScale = (int) Math.ceil(options.outHeight * 1.0 / maxSize);
            inSampleSize = Math.max(widthScale, heightScale);
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        bitmap = rotateBitmapDegree(bitmap,degree);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int newW = w;
        int newH = h;
        if (w > maxSize || h > maxSize) {
            if (w > h) {
                newW = maxSize;
                newH = (int) (newW * h * 1.0 / w);
            } else {
                newH = maxSize;
                newW = (int) (newH * w * 1.0 / h);
            }
        }
        Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, newW, newH, false);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(newPath);
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
        } catch (FileNotFoundException e) {

        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }
        recycle(newBitmap);
        recycle(bitmap);
        return newPath;
    }
    //endregion

    //region 回收图片 recycle(Bitmap bitmap)
    public static void recycle(Bitmap bitmap) {
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
        System.gc();
    }
    //endregion

    //region 得到图片角度 getImageRotate( String filePath )
    public static int getImageRotate( String filePath ) {
        int degree  = 0;

        try {
            ExifInterface exifInterface = new ExifInterface(filePath);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return degree;
    }
    //endregion

    //region 旋转图片角度 rotateBitmapDegree( Bitmap bmp, int degree )
    public static Bitmap rotateBitmapDegree( Bitmap bmp, int degree ) {
        Bitmap bitmap = null;

        if( degree != 0 ) {
            Matrix matrix = new Matrix();
            matrix.setRotate(degree);
            int width = bmp.getWidth();
            int height = bmp.getHeight();
            bitmap = Bitmap.createBitmap( bmp, 0, 0, width, height, matrix, true );
            bmp.recycle();
        }
        else {
            bitmap = bmp;
        }

        return bitmap;
    }
    //endregion

    public static String uuid() {
        StringBuilder sb = new StringBuilder();
        int start = 48, end = 58;
        appendChar(sb, start, end);
        appendChar(sb, 65, 90);
        appendChar(sb, 97, 123);
        String charSet = sb.toString();
        StringBuilder sb1 = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 24; i++) {
            int len = charSet.length();
            int pos = random.nextInt(len);
            sb1.append(charSet.charAt(pos));
        }
        return sb1.toString();
    }


    public static void appendChar(StringBuilder sb, int start, int end) {
        int i;
        for (i = start; i < end; i++) {
            sb.append((char) i);
        }
    }
}
